//https://leetcode.cn/problems/fruit-into-baskets/description/
//1
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0, right = 0;
        int n = fruits.size();
        vector<int> hash(n, 0);
        int count = 0, len = 0;
        while (right < n)
        {
            //jin chaung kou
            if (hash[fruits[right]] == 0)
                count++;
            hash[fruits[right]]++;

            //judge
            while (count > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                {
                    count--;
                }
                left++;
            }

            len = max(len, right - left + 1);
            right++;
        }
        return len;
    }
};


//2
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0, right = 0;
        unordered_map<int, int> hash;
        int ret = 0;
        while (right < fruits.size())
        {
            hash[fruits[right]]++;

            while (hash.size() > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                    hash.erase(fruits[left]);
                left++;
            }
            ret = max(ret, right - left + 1);
            right++;
        }
        return ret;
    }
};